package com.atguigu.mvc.controller;

import com.atguigu.mvc.entity.Employee;
import com.atguigu.mvc.entity.Student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
// @ResponseBody
// @Controller
public class Demo04ControllerParam {

    // @ResponseBody
    @RequestMapping("/test/request/param")
    public String testRequestParam(
            // @RequestParam 语法点 1：设置请求参数的名称获取请求参数你，从 Controller 方法的入参这里传入
            // @RequestParam 语法点 2：required 属性默认为 true，表示这个请求参数必须提供，如果不提供就会看到 400 错误
            // @RequestParam 语法点 3：defaultValue 属性，表示如果请求参数没有提供，那么就使用这个默认值
            // @RequestParam 语法点 4：如果请求参数名和方法的入参名相同，那么可以省略 @RequestParam 注解，直接使用方法入参名即可
            @RequestParam(value = "userName", defaultValue = "big tom") String userName,
            @RequestParam(value = "salary", required = false) Double salary,
            @RequestParam(value = "team") List<String> teamList,
            String deptName) {

        System.out.println("userName = " + userName);
        System.out.println("salary = " + salary);
        for (String team : teamList) {
            System.out.println("team = " + team);
        }
        System.out.println("deptName = " + deptName);

        return "ok request param";
    }

    // @ResponseBody
    @RequestMapping("/test/save/emp")
    public String testSaveEmp(Employee employee) {
        return employee.toString();
    }

    // @ResponseBody
    @RequestMapping("/send/{message}/good/{age}")
    public String testPathVariable(
            @PathVariable("age") Integer age,
            @PathVariable("message") String message) {
        return "OK message = " + message + " age = " + age;
    }

    // 把 Controller 方法的输出转换为 JSON 字符串，作为响应体返回
    // @ResponseBody
    @RequestMapping("/save/student")
    public Student testRequestBody(
            // 把请求体的 JSON 转换为 Student 对象，作为 Controller 方法的输入
            @RequestBody Student student) {
        return student;
    }

    // @ResponseBody
    @RequestMapping("/send/cookie/value")
    public String testCookieValue(@CookieValue("goodMessage") String goodMessage) {
        return "ok cookie goodMessage = " + goodMessage;
    }

    // @ResponseBody
    @RequestMapping("/get/request/header")
    public String testRequestHeader(@RequestHeader("Accept-Encoding") String acceptEncoding) {
        return "ok Accept-Encoding = " + acceptEncoding;
    }
}
